<?php

class Default_Form_User extends Zend_Form {
	const USERNAME_LABEL         = 'Username';
	const FULLNAME_LABEL         = 'Họ tên';
	const IMAGE_LABEL            = 'Ảnh đại diện';
	const EMAIL_LABEL            = 'Email';
	const PASSWORD_LABEL         = 'Password';
	const SEX_LABEL              = 'Giới tính';
	const BIRTHDAY_LABEL         = 'Ngày sinh';
	const ADDRESS_LABEL          = 'Địa chỉ';
	const CITY_LABEL          	 = 'Thành phố';
	const TEL_LABEL              = 'Điện thoại';
	
	const ENCODING               = 'UTF-8';
	const USERNAME_MIN_LENGTH    = 3;
	const USERNAME_MAX_LENGTH    = 32;
	const FULLNAME_MIN_LENGTH    = 0;
	const FULLNAME_MAX_LENGTH    = 50;
	const PASSWORD_MIN_LENGTH    = 6;
	const PASSWORD_MAX_LENGTH    = 32;
	const IMAGEVOLUMN_MAX        = 100;
	const IMAGE_EXT              = '(png, jpg, gif)';
	const IMAGE_HEIGHT_MIN       = 0;
	const IMAGE_HEIGHT_MAX       = 200;
	const IMAGE_WIDTH_MIN        = 0;
	const IMAGE_WIDTH_MAX        = 200;
	
	public function init() {
		
		$this->setName('user');
		$id = new Zend_Form_Element_Hidden('id');
		$id->addFilter('Int');

		//User name
		$username = self::addUsername();
        //Full name
        $fullname = self::addFullName();
        //Email
        $email = self::addEmail();
		//Password
		$password = self::addPassword();
		//Sex
		$sex= self::addSex();
		//Birthday
		$birthday = self::addBirthday();
		//Address
		$address = self::addAddress();
        //City
        $city = self::addCity();
		//Tel
		$tel = self::addTel();
        // Avatar	
        $uploadImage = self::addAvatar();	
       	//Button
       	$submit = new Zend_Form_Element_Submit('submit');
        $submit->setAttrib('id', 'submitbutton')
        ->removeDecorator('DtDdWrapper');
        $reset = new Zend_Form_Element_Reset('reset');
        $reset->setAttrib('id', 'resetbutton')
        ->removeDecorator('DtDdWrapper');
        $this->addElements(array($id, $username, $password, $email, $uploadImage, $fullname, $tel, $address, $city, $birthday, $sex, $submit, $reset));
        $this->addDisplayGroup(array('submit', 'reset'), 'buttons');
        $dg = $this->getDisplayGroup('buttons');
        $dg->removeDecorator('Fieldset');
        $dg->removeDecorator('HtmlTag');
	}
	
	/*
	 * Add username element
	 */
	protected  function addUsername() {
		//Alpha validate
		$usernameAlpha = new Zend_Validate_Alpha(false);
        //Length validate
        $usernameLength = new Zend_Validate_StringLength(self::USERNAME_MIN_LENGTH, self::USERNAME_MAX_LENGTH, self::ENCODING);
        //Not empty
        $usernameNotEmpty = new Zend_Validate_NotEmpty();
        $tb_name = new Admin_Model_DbTable_Users();
        $username = new Zend_Form_Element_Text('username');
        $username	->setLabel(self::USERNAME_LABEL)
        			->setAttrib('class','username, txtwidth200')
        			->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->setRequired(true)
        			->addValidator($usernameNotEmpty,true)
        			->addValidator($usernameLength)
        			->addValidator('regex', true, 
                       array(
                           'pattern'=>'/^[(a-zA-Z0-9\_)]+$/', 
                           'messages'=>array(
                               'regexNotMatch'=>self::USERNAME_LABEL." '%value%' chứa ký tự không hợp lệ."                           )
                       ))
        			->addValidator('Db_NoRecordExists', false, array(
         						'table' => $tb_name->getTbName(),
        						'field' => 'username',
         						'messages' =>array(
         						'recordFound' => self::USERNAME_LABEL." '%value%' đã tồn tại.")));
		return $username;
	}
	
	/*
	 * Add full_name element
	 */
	protected  function addFullName() {
		$fullnameAlpha = new Zend_Validate_Alpha(true);
        $fullnameNotEmpty = new Zend_Validate_NotEmpty();
        //Length validate
        $fullnameLength = new Zend_Validate_StringLength(self::FULLNAME_MIN_LENGTH, self::FULLNAME_MAX_LENGTH, self::ENCODING);
        $fullname = new Zend_Form_Element_Text('ho_ten');
        $fullname	->setLabel(self::FULLNAME_LABEL)
        			->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->setRequired(true)
        			->addValidator($fullnameNotEmpty, true)
        			->addValidator($fullnameLength)
        			->addValidator($fullnameAlpha);
        return $fullname;
	}

	/*
	 * Add avatar
	 */
	protected function addAvatar() {
		//avatar
       	$uploadImage = new Zendhlt_Form_Element_Upload('hinh_anh');
       	$uploadImage->setLabel(self::IMAGE_LABEL);
        return $uploadImage;
	}
	
	/*
	 * Add email element
	 */
	protected  function addEmail() {
		 $tb_name = new Admin_Model_DbTable_Users();
         $email = new Zend_Form_Element_Text('email');
         $emailNotEmpty = new Zend_Validate_NotEmpty();
         $email	->setLabel(self::EMAIL_LABEL)
         		->setRequired(true)
         		->addValidator($emailNotEmpty, true)
         		->addValidator('Db_NoRecordExists', false, array (
         						'table' => $tb_name->getTbName(),
        						'field' => 'email',
         						'messages' =>array(
         						'recordFound' => self::EMAIL_LABEL." '%value%' đã tồn tại.")))
         		->addFilter('StripTags')
         		->addFilter('StringTrim')
         		->addValidator(new Zendhlt_Validate_Email());
         return $email;
	}
	
	/*
	 * Add password element
	 */
	protected function addPassword() {
		//Not empty validate
		$passwordNotEmpty = new Zend_Validate_NotEmpty();
        //Length validate
        $passwordLength = new Zend_Validate_StringLength(self::PASSWORD_MIN_LENGTH, self::PASSWORD_MAX_LENGTH, self::ENCODING);
		 $password = new Zend_Form_Element_Password('password');
         $password	->setLabel(self::PASSWORD_LABEL)
         			->setAttrib('class','txtwidth200')
         			->addFilter('StripTags')
         			->addFilter('StringTrim')
         			->setRequired(true)
         			->addValidator($passwordNotEmpty,true)
         			->addValidator($passwordLength);
         return $password;
	}
	
	/*
	 * Add sex element
	 */
	protected function addSex() {
		 $sex = new Zend_Form_Element_Radio('gioi_tinh');
         $sex	->setLabel(self::SEX_LABEL)
         		->setSeparator('')
         		->setValue('male')
         		->addMultiOptions(array(
        					false => 'Nam',
        					true => 'Nữ'));
        return $sex;
	}
	
	/*
	 * Add birthday
	 */
	protected function addBirthday() {
		//Date validate
		$birthdayFormat = new Zend_Validate_Date(array(
									'format' => 'dd-mm-yy',));
		$birthday = new ZendX_JQuery_Form_Element_DatePicker('ngay_sinh');
		$birthday	->setLabel(self::BIRTHDAY_LABEL)
					->setJQueryParam('dateFormat', 'dd-mm-yy')
					->setJQueryParam('changeYear', 'true')
					->setJqueryParam('changeMonth', 'true')
					->setJqueryParam('regional', 'vi')
					->setJqueryParam('yearRange', "-80:-16")
					->setJqueryParam('defaultDate', "08-08-1988")
					->setDescription('Định dạng ngày tháng dd-mm-yy')
					->addValidator($birthdayFormat);
		return $birthday;
	}
	
	/*
	 * Add address element
	 */
	protected  function addAddress() {
		$notEmptyAddress = new Zend_Validate_NotEmpty();
		$address = new Zend_Form_Element_Text('dia_chi');
		$address->setLabel(self::ADDRESS_LABEL)
         		->addFilter('StripTags')
         		->addFilter('StringTrim')
         		->setRequired(true)
         		->addValidator($notEmptyAddress, true);
        
        return $address;
	}
	
	/*
	 * Add city element
	 */
	protected function addCity() {
		$notEmptyCity = new Zend_Validate_NotEmpty();
		$city = new Zend_Form_Element_Select('thanh_pho');
		$city ->setLabel(self::CITY_LABEL)
			  ->setRequired(true)
			  ->setRegisterInArrayValidator(false)
			  ->addValidator($notEmptyCity, true);
		
		return $city;
	}
	
	/*
	 * Add tel
	 */
	protected function addTel() {
		$notEmptyTelephone = new Zend_Validate_NotEmpty();
        $telephone = new Zend_Form_Element_Text('dien_thoai');
        $telValidator = new Zendhlt_Validate_Phone();
        $telephone	->setLabel(self::TEL_LABEL)
        			->addDecorator('Description', array('escape' => false) )
        			->setDescription('Nhập theo định dạng: (xxx)xxx-xxxx nếu 10 số hay (xxxx)xxx-xxxx nếu 11 số.<br/> Ví dụ: (098)999-9999 hay (0167)999-9999')
        			->addFilter('StripTags')
        			->addFilter('StringTrim')
        			->addValidator($notEmptyTelephone, true);
        return $telephone;
	}
	
}
